package com.html;

import java.text.ParseException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.model.xmlElement.output.OutputFormater;
import com.tool.DateUtils;

public class FormaterDataOutput {
	public static final Logger LOG = LoggerFactory.getLogger(FormaterDataOutput.class);
	
	private OutputFormater outputFormater;
	private String dataColumnValue;
	
	
	public FormaterDataOutput(OutputFormater outputFormater,
			String dataColumnValue) {
		super();
		this.outputFormater = outputFormater;
		this.dataColumnValue = dataColumnValue;
	}

	public void formaterData(){
		LOG.info(dataColumnValue);
		String methodName = outputFormater.getOutputFormatermethod();
		if(methodName != null && !"".equals(methodName)){
			String[] methodArray = methodName.split("\\|");
			int methodLength = methodArray.length;
			if(methodLength > 0){
				for(int i = 0;i < methodLength;i++){
					if(methodArray[i].equals("substring")){
						substring(i);
					}else if(methodArray[i].equals("split")){
						split(i);
					}else if(methodArray[i].equals("date")){
						date(i);
					}
				}
			}
		}
	}
	
	private void substring(int i){
		String methodParams = outputFormater.getOutputFormaterparams();
		if(methodParams != null && !"".equals(methodParams)){
			String[] outputFormaterParamsArray = methodParams.split("\\|");
			String param = outputFormaterParamsArray[i];
			String[] args = param.split(",");
			if(args.length == 1){
				dataColumnValue = dataColumnValue.substring(Integer.valueOf(args[0]));
			}else if(args.length == 2){
				dataColumnValue = dataColumnValue.substring(Integer.valueOf(args[0]),Integer.valueOf(args[1]));
			}
		}
	}
	
	private void split(int i){
		String methodParams = outputFormater.getOutputFormaterparams();
		if(methodParams != null && !"".equals(methodParams)){
			String[] outputFormaterParamsArray = methodParams.split("\\|");
			String param = outputFormaterParamsArray[i];
			String[] array = dataColumnValue.split(param);
			String outputFormaterSplitIndexParam = outputFormater.getOutputFormaterSplitIndexParam();
			int index = Integer.valueOf(outputFormaterSplitIndexParam.split("\\|")[i]);
			try{
				dataColumnValue = array[index];
			} catch(ArrayIndexOutOfBoundsException e){
				LOG.info("error-->" + dataColumnValue);
			}
		}
	}
	
	private void date(int i){
		String methodParams = outputFormater.getOutputFormaterparams();
		if(methodParams != null && !"".equals(methodParams)){
			String[] outputFormaterParamsArray = methodParams.split("\\|");
			String param = outputFormaterParamsArray[i];
			String[] paramArray = param.split(",");
			try{
				dataColumnValue = DateUtils.getSdf(paramArray[1]).format(DateUtils.getSdf(paramArray[0]).parse(dataColumnValue));
			} catch(ArrayIndexOutOfBoundsException e){
				LOG.info("error-->" + dataColumnValue);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
	}



	public OutputFormater getOutputFormater() {
		return outputFormater;
	}

	public void setOutputFormater(OutputFormater outputFormater) {
		this.outputFormater = outputFormater;
	}

	public String getDataColumnValue() {
		return dataColumnValue;
	}

	public void setDataColumnValue(String dataColumnValue) {
		this.dataColumnValue = dataColumnValue;
	}
	

}
